﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using FlatRedBall;
using FlatRedBall.Graphics;
using FlatRedBall.Math.Geometry;
using FlatRedBall.Input;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

using FlatRedBall.Graphics.Animation;

using ROMAP.Utilities;

namespace ROMAP.Entities.Enemies
{
    public class FlyingEnemy : Enemy 
    {
        private Sprite _sprite;
        private float travelDistance;
        private bool horizontal;
        private bool pulsable;

        public float TravelDistance
        {
            get { return travelDistance; }
            set { travelDistance = value; }
        }

        private String PATH = @"Content/Enemies/FlyingEnemy/";
        private int NUM_FRAMES = 2;
        private float FRAME_TIME = 0.1f;

        public FlyingEnemy(int x, int y, float travelDistance, bool horizontal)
        {
            TilePosition = new Vector2i(x, y);
            xstart = X;
            ystart = Y;
            this.travelDistance = travelDistance;
            this.horizontal = horizontal;
            Initialize(true);
        }

        protected virtual void Initialize(bool addToManagers)
        {
            base.Initialize();

            AnimationChainList ani = new AnimationChainList();
            AnimationChain aniLeft = InitializeAnimation(PATH + "Left/", NUM_FRAMES, FRAME_TIME);
            AnimationChain aniRight = InitializeAnimation(PATH + "Right/", NUM_FRAMES, FRAME_TIME);
            AnimationChain aniUp = InitializeAnimation(PATH + "Up/", NUM_FRAMES, FRAME_TIME);
            AnimationChain aniDown = InitializeAnimation(PATH + "Down/", NUM_FRAMES, FRAME_TIME);
            AnimationChain idle = InitializeAnimation(PATH + "Idle/", 2, FRAME_TIME);

            ani.Add(aniLeft); ani.Add(aniRight);
            ani.Add(aniUp); ani.Add(aniDown);
            ani.Add(idle);

            aniLeft.Name = "left"; aniRight.Name = "right";
            aniUp.Name = "up"; aniDown.Name = "down";
            idle.Name = "idle";

            _sprite = SpriteManager.AddSprite(ani);

            _sprite.CurrentChainName = "idle";

            pulsable = true;

            _sprite.AttachTo(this, false);
            _sprite.PixelScale();
            _sprite.ScaleX *= .5f;
            _sprite.ScaleY *= .5f;
            _sprite.RelativeZ = 0.05f;

            _sprite.Visible = true;

            Collision = ShapeManager.AddAxisAlignedRectangle();
            Collision.AttachTo(this, false);
            Collision.ScaleX = _sprite.ScaleX * .6f;
            Collision.ScaleY = _sprite.ScaleY * .6f;
            Collision.Visible = false;

            PulseCollision = ShapeManager.AddAxisAlignedRectangle();
            PulseCollision.AttachTo(this, false);
            PulseCollision.ScaleX = _sprite.ScaleX * 1.0f;
            PulseCollision.ScaleY = _sprite.ScaleY * 1.0f;
            PulseCollision.Visible = false;
        }

        public override void Activity()
        {
            if (horizontal)
            {
                if (travelDistance > 0)
                {
                    if (Math.Abs(this.xstart - this.X) < travelDistance)
                    {
                        this.Acceleration.X = 10.0f;
                        _sprite.CurrentChainName = "right";
                    }
                    else
                    {
                        this.Acceleration.X = -10.0f;
                        _sprite.CurrentChainName = "left";
                    }
                    if (Math.Abs(this.xstart - this.X) > 1.9f * travelDistance)
                        this.Velocity.X = 0.0f;
                }
                else if (travelDistance < 0)
                {
                    if (Math.Abs(this.xstart - this.X) < -travelDistance)
                    {
                        this.Acceleration.X = -10.0f;
                        _sprite.CurrentChainName = "left";
                    }
                    else
                    {
                        this.Acceleration.X = 10.0f;
                        _sprite.CurrentChainName = "right";
                    }
                    if (Math.Abs(this.xstart - this.X) > -1.9f * travelDistance)
                        this.Velocity.X = 0.0f;
                }
                    
            }
            else
            {
                if (travelDistance > 0)
                {
                    if (Math.Abs(this.ystart - this.Y) < travelDistance)
                    {
                        this.Acceleration.Y = 10.0f;
                        _sprite.CurrentChainName = "up";
                    }
                    else
                    {
                        this.Acceleration.Y = -10.0f;
                        _sprite.CurrentChainName = "down";
                    }
                }
                else if (travelDistance < 0)
                {
                    if (Math.Abs(this.ystart - this.Y) < -travelDistance)
                    {
                        this.Acceleration.Y = -10.0f;
                        _sprite.CurrentChainName = "down";
                    }
                    else
                    {
                        this.Acceleration.Y = 10.0f;
                        _sprite.CurrentChainName = "up";
                    }
                }
            }
        }

        public override void Destroy()
        {
            base.Destroy();

            SpriteManager.RemoveSprite(_sprite);
        }

        protected AnimationChain InitializeAnimation(string path, int numFrames, float frameTime)
        {
            // Loads the animation chain
            AnimationChain chain = new AnimationChain();

            for (int i = 1; i <= numFrames; i++)
            {
                AnimationFrame frame = new AnimationFrame(path + i, frameTime, "Global");

                chain.Add(frame);
            }

            return chain;
        }
    }

}
